package com.lagou.handler;

import com.alibaba.fastjson.JSONObject;
import com.lagou.service.RpcRequest;
import com.lagou.service.UserServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.lang.reflect.Method;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class UserServerHandler extends ChannelInboundHandlerAdapter {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("service----------------");
        // 判断是否符合约定，符合则调用本地方法，返回数据
        // msg:  UserService#sayHello#are you ok?
        if (msg instanceof RpcRequest) {
            RpcRequest rpcRequest = (RpcRequest) msg;
            String implClassName = getImplClassName(rpcRequest);
            System.out.println(rpcRequest.getMethodName());
            String className = rpcRequest.getClassName();
            Class<?> aClass = Class.forName(className);
            Map<String, ? extends Object> beansOfType = applicationContext.getBeansOfType(aClass);
            int size = beansOfType.size();
            if (size > 1) {
                ctx.writeAndFlush("未指定实现类");
            }
            for (String key : beansOfType.keySet()) {
                Object o = beansOfType.get(key);
                String name = o.getClass().getName();
                Class<?> aClass1 = Class.forName(name);
                Method method1 = aClass1
                        .getMethod(rpcRequest.getMethodName(), rpcRequest.getParameterTypes());
                Object invoke = method1.invoke(aClass1.newInstance(),
                        rpcRequest.getParameters());
                System.out.println(invoke);
                ctx.writeAndFlush(invoke);
            }
         /*   int lastDot = rpcRequest.getClassName().lastIndexOf(".");
            String interfaceName = rpcRequest.getClassName().substring(0, lastDot);
            String a = interfaceName + ".UserServiceImpl";
            Class userService = Class.forName(interfaceName + ".UserServiceImpl");
            Method method1 = userService
                    .getMethod(rpcRequest.getMethodName(), rpcRequest.getParameterTypes());
            Object invoke = method1.invoke(userService.newInstance(),
                    rpcRequest.getParameters());
            System.out.println(invoke);*/

        }
        if (msg.toString().startsWith("UserService")) {
            System.out.println("dffffffffffffffff");
            UserServiceImpl userService = new UserServiceImpl();
            String result = userService
                    .sayHello(msg.toString().substring(msg.toString().lastIndexOf("#") + 1));
            ctx.writeAndFlush(result);
        }
        System.out.println("fdafasdffffasdfasdfas");
        if (msg instanceof JSONObject) {
            System.out.println("dfasdfffffffffffffffaaaaaaaaaaaaaa");
            UserServiceImpl userService = new UserServiceImpl();
            RpcRequest rpcRequest = (RpcRequest) msg;
            JSONObject jsonObject = new JSONObject();
            String request = userService.getRequest(jsonObject);
            ctx.writeAndFlush(request);
        }


    }


    private String getImplClassName(RpcRequest rpcRequest) throws Exception {
        System.out.println(rpcRequest.getClassName());
        return rpcRequest.getClassName();
    }
}
